<template>
  <div>
    <div>
      <el-card>
        <div class="clearfix">
          <span>角色管理</span>
        </div>
        <div class="set">
          <el-button size="big" type="primary" @click="addq">添加</el-button>
        </div>

        <el-table
          size="mini"
          @selection-change="handleSelectionChange"
          :data="getrole"
          border
          style="width: 100%"
        >
          <el-table-column type="selection" width="55"> </el-table-column>
          <el-table-column prop="id" label="id" width="110"></el-table-column>
          <el-table-column
            prop="name"
            label="登录账号"
            width="110"
          ></el-table-column>
          <el-table-column label="操作" width="180px">
            <template slot-scope="scoped">
              <el-button size="big" type="success">修改</el-button>
              <el-button type="danger" @click="open(scoped.row.id)"
                >删除</el-button
              >
            </template>
          </el-table-column>
        </el-table>
        <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="page"
          :page-sizes="[4, 10, 15, 20]"
          :page-size="page"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total"
        >
        </el-pagination>
      </el-card>
    </div>

    <!-- 点击添加后打开的东西 -->
    
    <el-dialog
  title="提示"
  :visible.sync="dialogVisible"
  width="50%">
  <el-form ref="form" :model="form" label-width="80px">
        <el-form-item label="账号 ">
          <el-input v-model="addForm.name"></el-input>
        </el-form-item>
      <div class="center" v-for="item in checkedCities" :key="item.label">
        <el-checkbox
          :value="!!item.checked"
          @change="handleCheckAllChange($event, item)"
          :label="item.name"
        ></el-checkbox>
        <el-checkbox-group
          v-model="checkedArr"
          @change="handleCheckedCitiesChange($event, item)"
        >
          <el-checkbox v-for="sub in item.children" :label="sub.id" :key="sub.id">{{
            sub.name
          }}</el-checkbox>
        </el-checkbox-group>
      </div>
    </el-form>
  <span slot="footer" class="dialog-footer">
    <el-button @click="dialogVisible = false">取 消</el-button>
    <el-button type="primary" @click="addjue">确 定</el-button>
  </span>
</el-dialog>
  </div>
</template>
<script>
import { lis, juese, addjuese, detrooms,} from "../../utils/utils";
import axios from 'axios'

export default {
  data() {
    return {
      addForm: {
        id: 0, // 0-添加 大于0修改
        name: "",
        menus: [],
      },
      checkedArr:[],
      checkAll: false,
      checkedCities: [],
      cities: [],
      isIndeterminate: true,
      page: 1,
      pageSize: 5,
      total: 1, //分页
      getrole: [],
      getroledeldte: [],
      form: {
        type: [],
      },
      query: {},
      total: 0,
      multipleSelection: [],
      dialogVisible: false,
    };
  },
  methods: {
    async addjue(){
      let {errCode}=await addjuese(this.addForm)
      if(errCode=='10000'){
        this.$message({
          type:'success',
          message:'添加成功'
        })
        this.dialogVisible=false
        this.getroles()
      }else{
        this.$message({
          type:'error',
          message:'添加失败'
        })
      }
    },
    async addq() {
      this.dialogVisible = true;
      let res = await juese();
      this.checkedCities = res.data.list;
    },
    handleCheckAllChange(bool, item) {
      // console.log(bool);
      console.log(item);
      let arr=item.children.map(item=>item.id)
      item.checked = bool
      if(bool){
        if(item.children){
          this.checkedArr=[...new Set(this.checkedArr.concat(arr))]
        }
      }
      else{
          this.checkedArr = this.checkedArr.filter(item => !arr.includes(item))
        }
    },
    handleCheckedCitiesChange(arr,data) {
      console.log(arr, data)
      data.checked = data.children.filter(item => arr.includes(item.id)).length === data.children.length
    },
    handleSelectionChange(val) {
      console.log(val);
    },
    //分页
    handleSizeChange(val) {
      this.pageSize = val;
      this.getroles();
    },
    handleCurrentChange(val) {
      this.page = val;
      this.getroles();
    },

    onSubmit() {
      console.log("submit!");
    },
    //角色数据渲染  分页内容
    async getroles() {
      const res = await lis({
        page: this.page,
        psize: this.pageSize,
      });
      console.log("2222", res);
      this.getrole = res.data.list;
      this.total = res.data.counts;
      // console.log(res);
    },
    //单选删除
    // async open(id) {
    //   console.log(id);
    //   let res=await detrooms(id)
    //   console.log(res);
    // },
    open(id){
      this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
           axios.get('http://estate.eshareedu.cn/estate/api/administratorrole/delete',{params:{id:id}}).then(res=>{
            // console.log(res);
             this.getroles();
          })
          this.$message({
            type: 'success',
            message: '删除成功!'
          });
        }).catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          });          
        });
    },
  },

  created() {
    this.getroles();
  },
};
</script>
<style lang="less" scoped>
.el-row {
  border-bottom: 1px solid #eee;
  height: 70px;
  line-height: 70px;
}
.clearfix {
  padding-bottom: 10px;
  border-bottom: 1px solid #eee;
}
.clearfix span {
  color: blue;
}
.el-input {
  width: 30%;
  height: 30px;
  font-size: 12px;
}

.el-select {
  width: 35%;
  height: 30px;
  font-size: 12px;
}
.set {
  margin-top: 20px;
}
.el-table {
  font-size: 12px;
  margin: 10px 0px;
}
.el-breadcrumb {
  margin: 10px 0px;
}
.addright {
  float: right;
}
.el-tag {
  margin: 15px 10px;
}
.el-col-7 {
  width: 27%;
}
.el-table[data-v-09d83b62] {
  font-size: 18px;
  margin: 10px 0px;
}
.center {
  width: 100%;
  height: 100px;
  // background-color: red;
}
.left {
  width: 50%;
  // height: 20px;
  float: left;
  // background-color: pink;
}
.right {
  width: 50%;
  // height: 20px;
  float: right;
  // background-color: rgb(17, 15, 15);
}
</style>
